package com.zzzyh.forumsystemclient.cotroller;

import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.dao.UserMapper;
import com.zzzyh.forumsystemclient.model.*;
import com.zzzyh.forumsystemclient.services.IChatService;
import com.zzzyh.forumsystemclient.services.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private IChatService chatService;
    @Autowired
    private IUserService userService;
    @RequestMapping("/list")
    public AppResult<PageResult<ChatMsg>> list(@RequestParam Integer pageNum,
                                               @RequestParam Integer pageSize,
                                               @RequestParam Integer roomId){
        List<ChatMsg> chatMsgs = chatService.list(pageNum, pageSize, roomId);
        for (ChatMsg chatMsg : chatMsgs) {
            chatMsg.setName(userService.getUserinfoByUserId(chatMsg.getSenderId()).getNickname());
            chatMsg.setAvatar(userService.getUserinfoByUserId(chatMsg.getSenderId()).getAvatar());
            chatMsg.setTime(chatMsg.getCreateTime());
        }
        Integer total = chatService.total();
        return AppResult.success(new PageResult<>(pageNum, pageSize,total, chatMsgs));
    }
    @RequestMapping("/sendMsg")
    public AppResult<String> send(@RequestBody ChatMsg chatMsg){
        chatService.send(chatMsg);
        return AppResult.success("发送成功");
    }
    @RequestMapping("/friendsList/{userID}")
    public AppResult<List<ChatFriend>> friendsList(@PathVariable Integer userID){

        List<ChatFriend> chatFriends = new java.util.ArrayList<>();
        List<Integer> friendsList = new ArrayList<>();
        if (userID != 1){
             friendsList=chatService.getFriendsList(userID);
             friendsList.add(1);
        }else {
            friendsList=chatService.getAdminFriendsList();
        }
        for (Integer friendID : friendsList){
            ChatFriend chatFriend = new ChatFriend();
            User user = userService.getUserinfoByUserId(friendID);
            chatFriend.setId(friendID);
            chatFriend.setName(user.getUsername());
            chatFriend.setAvatar(user.getAvatar());
            if(user.getSex()!= null && user.getSex() == 1)
                chatFriend.setSignature("男");
            else if(user.getSex()!= null && user.getSex() == 2)
                chatFriend.setSignature("女");
            chatFriend.setSignature(user.getSignature());
            chatFriends.add(chatFriend);
        }
        return AppResult.success(chatFriends);
    }

    @RequestMapping("/rooms/{userID}")
    public AppResult<List<ChatRoom>> rooms(@PathVariable Integer userID){
        List<ChatRoom> chatRooms = chatService.rooms(userID);
        for (ChatRoom chatRoom : chatRooms) {
            chatRoom.setUnreadCount(chatService.getUnreadCount(chatRoom.getId(), userID));
        }
        return AppResult.success(chatRooms);
    }

    @RequestMapping("/roomWithFriend/{userId}/{friendId}")
    public AppResult<ChatRoom> roomWithFriend(@PathVariable Integer userId, @PathVariable Integer friendId){
        return AppResult.success(chatService.roomWithFriend(userId, friendId));
    }

    @RequestMapping("/friendInfo/{friendId}")
    public AppResult<ChatFriend> friendInfo(@PathVariable Integer friendId){
        ChatFriend chatFriend = new ChatFriend();
        User user = userService.getUserinfoByUserId(friendId);
        chatFriend.setId(friendId);
        chatFriend.setName(user.getUsername());
        chatFriend.setAvatar(user.getAvatar());
        if(user.getSex()!= null && user.getSex() == 1)
            chatFriend.setSignature("男");
        else if(user.getSex()!= null && user.getSex() == 2)
            chatFriend.setSignature("女");
        chatFriend.setSignature(user.getSignature());
        return AppResult.success(chatFriend);
    }
    @RequestMapping("/lastmsg/{roomId}")
    public AppResult<ChatMsg> lastmsg(@PathVariable Long roomId){
        ChatMsg chatMsg = chatService.getLastMsg(roomId);
        return AppResult.success(chatMsg);
    }

    @RequestMapping("recallMsg")
    public AppResult<String> recallMsg(@RequestBody Map<String, String> requestData){
        chatService.recallMsg(Integer.valueOf(requestData.get("id")));
        return AppResult.success("撤回成功");
    }

    @RequestMapping("/updateUnreadCount/{roomId}/{unreadCount}/{userId}")
    public AppResult<String> updateUnreadCount(@PathVariable Long roomId, @PathVariable Integer unreadCount, @PathVariable Integer userId){
        chatService.updateUnreadCount(roomId, userId, unreadCount);
        return AppResult.success("更新成功");
    }



}
